package com.cw.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cw.common.Constants;
import com.cw.common.R;
import com.cw.config.JwtConfig;
import com.cw.entity.Category;
import com.cw.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增菜品  (数据由前端传输进来，更改时间等信息自动配置完成)
     * @param category
     * @return
     */
    @PostMapping
    public R save(@RequestBody Category category,
                  HttpServletRequest request) {
        if(!JwtConfig.getCurrentId(request).equals(Constants.ADMIN_ID)){
            return R.error("请提升至管理员权限");
        }
        log.info("接收到数据{}",category);

        boolean b = categoryService.save(category);

        return b?R.success("添加成功"):R.error("添加失败");
    }

    /**
     * 分页查询菜品
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R page(@RequestParam(name = "page") Integer page,
                  @RequestParam(name = "pageSize") Integer  pageSize,
                  HttpServletRequest request){
        if(!JwtConfig.getCurrentId(request).equals(Constants.ADMIN_ID)){
            return R.error("请提升至管理员权限");
        }
        log.info("page:{},pageSize:{}",page,pageSize);

        //1.设置分页构造器
        IPage<Category> p = new Page<>(page,pageSize);
        //2.设置查询条件
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        //2.1设置排序顺序
        wrapper.orderByAsc(Category::getSort ).orderByDesc(Category::getUpdateTime);
        //3.添加分页查询业务
        categoryService.page(p, wrapper);

        return R.success(p);
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping
    public  R delete(@RequestParam(name = "id") Long id,
                     HttpServletRequest request){
        if (!JwtConfig.getCurrentId(request).equals(Constants.ADMIN_ID)){
            return R.error("请提升至管理员权限");
        }
        categoryService.delete(id);
        return R.success("分类信息删除成功");
    }

    /**
     * 修改分类信息
     * @param category
     * @return
     */
    @PutMapping
    public R update(@RequestBody Category category,
                    HttpServletRequest request) {
        log.info("category:{}",category);
        if (!JwtConfig.getCurrentId(request).equals(Constants.ADMIN_ID)){
            return R.error("请提升至管理员权限");
        }
        boolean b = categoryService.updateById(category);

        return b?R.success("修改成功"):R.error("修改失败");
    }


    /**
     * 菜品分类
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R list(Category category){
        //1.接收参数
        log.info("category:{}",category);
        //2.创建条件
        LambdaQueryWrapper<Category> wr = new LambdaQueryWrapper<>();
        wr.eq(category.getType()!=null,Category::getType,category.getType());
        wr.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        List<Category> list = categoryService.list(wr);
        return R.success(list);
    }
}
